const Mock = require('mockjs')
const bodyParser = require('body-parser')
const JWT = require('jsonwebtoken')
const keys = 'werwrqeq'
const users = [{
  username: 'admin',
  password: 'admin',
  role: 'admin'
},{
  username: 'zhangsan',
  password: 'zhangsan',
  role: 'teacher'
},{
  username: 'lisi',
  password: 'lisi',
  role: 'assistant'
}]
module.exports = app => {
  // 解析json 数据
  app.use(bodyParser.json());
  // 提供接口
  app.post('/api/login', (req, res) => {
    // 获取用户名和密码
    const { username, password } = req.body;
    const data = users.find(v => v.username === username && v.password === password);
    if(data) {
      res.send({
        code: 200,
        message: '登录成功， 欢迎' + data.username,
        data: {
          token: JWT.sign({ ...data }, keys),
          role: data.role
        }
      })
    } else {
      res.send({
        code: 400,
        message: '登录失败, 请重新登录'
      })
    }
  })
}